package com.ctg.itrdc.sysmgr.portal.control;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.sysmgr.portal.common.constant.DictEnum.ReturnCodeDefine;
import com.ctg.itrdc.sysmgr.portal.common.entity.CommonResult;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

@ControllerAdvice
public class ExceptionController {

	@ExceptionHandler
	@ResponseBody
	public CommonResult exp(Exception ex) {

		// LOGGER.error(ex.getCause().getMessage());
		ex.printStackTrace();
				
		if (ex instanceof DuplicateKeyException
				|| ex instanceof MySQLIntegrityConstraintViolationException) {
			return new CommonResult(ReturnCodeDefine.DUPLICATE_KEY_EXCEPTION.toString(),ex.getMessage());
		}
		return new CommonResult(HttpStatus.INTERNAL_SERVER_ERROR.toString(),
				ex.getMessage());
	}
}
